package com.miao.manage.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.miao.entity.PageVo;
import com.miao.entity.TbItem;
import com.miao.entity.TbItemDesc;
import com.miao.service.TbItemDescService;
import com.miao.service.TbItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author shuangshuang
 * @since 2018-09-07
 */
@Controller
@RequestMapping("/tbItem")
public class TbItemController {

    //日志
    private static final Logger LOGGER = LoggerFactory.getLogger(TbItemCatController.class);
    @Autowired
    private TbItemService tbItemService;
    @Autowired
    private TbItemDescService tbItemDescService;
    /**
     * 新增商品
     * @param tbItem
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> savaItem(TbItem tbItem, @RequestParam("desc") String desc){
        try {
            //判断日志级别
            if(LOGGER.isDebugEnabled()){
                LOGGER.debug("新增商品，tbItem={},desc={}",tbItem,desc);
            }
            if(StringUtils.isEmpty(tbItem)){
              //为空
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            //初始值
            tbItem.setStatus(1);
            tbItem.setCreated(new Date());
            tbItem.setUpdated(tbItem.getCreated());
            //出于安全考虑强制设置ID为空，通过数据库自增长得到ID
            tbItem.setId(null);
            boolean insert = this.tbItemService.insert(tbItem);
            //添加商品描述
            TbItemDesc tbItemDesc = new TbItemDesc();
            //拿到ID
            tbItemDesc.setItemId(tbItem.getId());
            tbItemDesc.setItemDesc(desc);
            this.tbItemDescService.insert(tbItemDesc);
            //判断日志级别
            if(LOGGER.isDebugEnabled()){
                LOGGER.info("新增成功！，tbItem={}",tbItem.getId());
            }
            //添加成功
            return ResponseEntity.status(HttpStatus.CREATED).build();
        }catch (Exception e){
            LOGGER.error("新增商品失败！tbItem="+tbItem,e);
        };
        //添加失败！
      return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).build();

    }

        /**
         * 分页查询 默认为第一页
         * @return
         */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<PageVo> query(@RequestParam(defaultValue = "1",name = "page") Integer pageNum,Integer rows) {
        Page<TbItem> tbItemPage = new Page<>(pageNum, rows);
        EntityWrapper<TbItem> wrapper = new EntityWrapper<>();
        wrapper.where("status={0}",1).or("status={0}",2);
        wrapper.orderBy("created DESC");
        Page<TbItem> tbItemPage1 = tbItemService.selectPage(tbItemPage, wrapper);
        //EasyUI 分页插件
        PageVo<TbItem> pageVo = new PageVo<>();
        pageVo.setTotal((int)tbItemPage1.getTotal());
        pageVo.setRows(tbItemPage1.getRecords());
        return ResponseEntity.ok(pageVo);

    }

    /**
     * 修改商品信息
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updataItem(TbItem tbItem, @RequestParam("desc") String desc){
        try {
            //判断日志级别
            if(LOGGER.isDebugEnabled()){
                LOGGER.debug("编辑商品，tbItem={},desc={}",tbItem,desc);
            }
            if(StringUtils.isEmpty(tbItem)){
                //为空
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            //修改时间
            tbItem.setUpdated(new Date());
            //修改商品信息
            boolean insert = this.tbItemService.updateById(tbItem);

            //编辑商品描述
            EntityWrapper<TbItemDesc> wrapper = new EntityWrapper<>();
            wrapper.where("item_id={0}",tbItem.getId());
            TbItemDesc tbItemDesc = new TbItemDesc();
            tbItemDesc.setItemDesc(desc);
            tbItemDesc.setUpdated(new Date());
            this.tbItemDescService.update(tbItemDesc,wrapper);

            if(!insert){
                if(LOGGER.isInfoEnabled()){
                   LOGGER.info("编辑商品失败，tbItem={},desc={}",tbItem,desc);
                }
                //保存失败500
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
            //判断日志级别
            if(LOGGER.isInfoEnabled()){
                LOGGER.info("编辑成功！，tbItem={}",tbItem.getId());
            }

            //编辑成功204
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            LOGGER.error("编辑商品失败！tbItem="+tbItem,e);
        };
        //编辑失败！
        return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).build();
    }


    /**
     * 下架
     * @param id
     * @return
     */
    @RequestMapping(value = "/instock")
    public ResponseEntity<Void> upItem(String id){
        TbItem tbItem = new TbItem();
        tbItem.setStatus(2);
        EntityWrapper<TbItem> wrapper = new EntityWrapper<>();
        wrapper.where("id in("+id+")");
        tbItemService.update(tbItem,wrapper);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 上架
     * @param id
     * @return
     */
    @RequestMapping(value = "/reshelf")
    public ResponseEntity<Void> upreshelf(String  id){
        TbItem tbItem = new TbItem();
        tbItem.setStatus(1);
        EntityWrapper<TbItem> wrapper = new EntityWrapper<>();
        wrapper.where("id in("+id+")");
        tbItemService.update(tbItem,wrapper);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    public ResponseEntity<Void> updelete(String id){
        TbItem tbItem = new TbItem();
        tbItem.setStatus(3);
        EntityWrapper<TbItem> wrapper = new EntityWrapper<>();
        wrapper.where("id in("+id+")");
        tbItemService.update(tbItem,wrapper);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}

